home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / comm / misc / atomic1_5.lha / atomic.rexx < prev    next >
OS/2 REXX Batch file  |  1999-01-03  |  9KB  |  385 lines

  1. /*     * * * * *    A T O M I C . R E X X     * * * * *           */
  2. /*                                                                */
  3. /*      file: s:atomic.rexx                                       */
  4. /*                                                                */
  5. /*      by John Chufar                                            */
  6. /*      ver 1.5  23 Dec 1998                                      */
  7. /*      *Minor enhancements:                                      */
  8. /*      -Fixes Time Difference Calculation bug                    */
  9. /*      -Fixes Time Zone hour offset report ( +/- offset)         */
  10. /*                                                                */
  11. /* ************************************************************** */
  12.  
  13.  
  14.  
  15. startup:
  16.  
  17. /* init the command channel to AmigaDOS and see if TCP is mounted */
  18.  
  19. ADDRESS COMMAND
  20.  
  21. " Assign >NIL: TCP: exists "
  22.  
  23. IF RC = 5 THEN
  24.    DO
  25.       SAY ' TCP: not mounted ... exiting  '
  26.       EXIT 10
  27.    END
  28.  
  29. startupend:
  30.  
  31.  
  32.  
  33. debug:
  34.  
  35. /* Use TRACE only for debug--it is very time consuming! */
  36. /* TRACE(I)  */
  37.  
  38. debugend:
  39.  
  40.  
  41.  
  42. titlescreen:
  43.  
  44. /* display the title screen ...   */
  45.  
  46. SAY ''
  47. SAY '                 * * *   A T O M I C L O C K  * * * '
  48. SAY '                             Version 1.5            '
  49. SAY ''
  50. SAY ''
  51.  
  52. /* dst$ set module */
  53.  
  54. numargs = ARG()  /* number of args */
  55.  
  56. IF numargs < 1 THEN
  57.    DO
  58.       SAY ''
  59.       SAY 'Daylight savings time? (Y or N) Then <ENTER> '
  60.       PULL dst$
  61.    END
  62.   ELSE
  63.    DO
  64.       ARG dst$
  65.    END
  66.  
  67.  
  68. SAY 'Connecting to Atomic Clock server time-A.timefreq.bldrdoc.gov...'
  69.  
  70. titlescreenend:
  71.  
  72.  
  73.  
  74. connect:
  75.  
  76. /* open the tcp channel to the host */
  77.  
  78. IF ~OPEN(dt,'TCP:time-A.timefreq.bldrdoc.gov/daytime', 'R') THEN
  79.       DO
  80.         SAY 'Connection error with TIMESERVER - A'
  81.         SAY 'Retrying  now  with   TIMESERVER - B'
  82.         SAY ''
  83.         SAY 'Attempting connection to time-B.timefreq.bldrdoc.gov'
  84.         IF ~OPEN(dt,'TCP:time-B.timefreq.bldrdoc.gov/daytime', 'R') THEN
  85.           DO
  86.             SAY '*** NIST Time Services are not present...exiting'
  87.             SAY ''
  88.             SAY 'Try again at a later time.'
  89.             SAY '**************************************'
  90.             EXIT 10
  91.           END
  92.       END
  93.  
  94. connectend:
  95.  
  96.  
  97.  
  98. decode:
  99. /* get the atomic time from the open file 'dt'  */
  100.   
  101. d = readln(dt) /* dummy read...empty first line */
  102. d = readln(dt)
  103.  
  104.  
  105. tim = SubWord(d, 3, 1)
  106.  
  107. utcdate=SubWord(d,2,1)
  108.  
  109. utcday=SubStr(utcdate,7,2)
  110. utcmonth=SubStr(utcdate,4,2)
  111. utcyear=SubStr(utcdate,1,2)
  112.  
  113. decodend:
  114.  
  115.  
  116.  
  117. oldtime = TIME('N')
  118.  
  119. /* changed in 1.5 from DATE('B') */
  120.  
  121. olddate = DATE('I')
  122.  
  123. Date TIME tim DATE utcday'-'utcmonth'-'utcyear
  124.  
  125.  
  126. /*      Script to offset the time to local                        */
  127. /*      and calculates your localtime 'error'                     */
  128.  
  129.  
  130. /* This section checks to see if locale.prefs exists */
  131.  
  132. IF ~(Exists('env:sys/locale.prefs'))
  133.    THEN DO
  134.      Say 'Fatal error *** Env:sys/locale.prefs does not exist ***'
  135.      Exit 10
  136.    END
  137.  
  138. /* put the info into a text file to read */
  139. 'type env:sys/locale.prefs hex >ram:locale.txt'
  140.  
  141. /* Read in the hex values for timezone from the locale.txt file */
  142. IF ~Open(st,'ram:locale.txt', 'R')
  143.    THEN DO
  144.       Say '*** Unable to create temp file'
  145.       Exit 10
  146.    END
  147.  
  148.  
  149. /* move to the point in the file where the timezone info is */
  150.  
  151. dummy = SEEK(st,1614,'B')
  152. b = READCH(st,4)
  153.  
  154. /* get all hex characters up to the point */
  155. /* of the where the two values are that hold the timezone */
  156.  
  157. /* convert the characters to their decimal equivalent  */
  158. tzoval = X2D(b)
  159.  
  160. gmtflag = 0
  161.  
  162. IF tzoval > 660      /* if the timeszone is > international date line */
  163.    THEN DO
  164.    tzoval = 65536 - tzoval       /* reverse offset (12 hours or greater */
  165.    tzocalc = tzoval/60           /* convert to hours.decimins */
  166.    tzocalc = TRUNC(tzocalc,2)    /* hours.mm format */
  167.    tzohour = TRUNC(tzocalc)      /* hours */
  168.    tzomin  = RIGHT(tzocalc,2)    /* extract minutes */
  169.    tzomin  = tzomin * .6         /* convert decimal to minutes */
  170.    tzomin  = TRUNC(tzomin)       /* discard decimals (seconds) */
  171.    tzohour = 24 - tzohour        /* complete the offset */
  172.    if tzomin > 0 THEN tzohour = tzohour -1
  173.    gmtflag = 1
  174.    END
  175.  
  176.  
  177. IF gmtflag = 0      /* if the timezone is < 12 hours fom GMT */
  178.    THEN DO
  179.    tzocalc = tzoval/60           /* convert to hours.decimins */
  180.    tzocalc = TRUNC(tzocalc,2)    /* hours.mm format */
  181.    tzohour = TRUNC(tzocalc)      /* hours */
  182.    tzomin  = RIGHT(tzocalc,2)    /* extract minutes */
  183.    tzomin  = tzomin * .6         /* convert decimal to minutes */
  184.    tzomin  = TRUNC(tzomin)       /* discard decimals (seconds) */
  185.    END
  186.  
  187. IF dst$ = 'Y' THEN tzohour = tzohour - 1  /* read dst$ in ARGS */
  188.  
  189. tzoff$ = "-" || tzohour
  190.  
  191. IF tzohour > 12
  192.    THEN DO
  193.    tzoff = 24 - tzohour
  194.    IF tzomin > 0 THEN tzoff = tzoff -1  /* Compensate for half hour zones */
  195.    tzoff$ = "+" || tzoff
  196.    END
  197.  
  198. SAY 'Time adjusted '|| tzoff$ ||' hours and ' tzomin ' minutes from UTC.'
  199. SAY ''
  200.  
  201.  
  202. /* read current system time and extract hours and minutes */
  203.  
  204. timestring = TIME('N')
  205. mins = SUBSTR(timestring,4,2)
  206. hour = LEFT(timestring,2)
  207.  
  208. newmins = mins - tzomin
  209. IF newmins < 0
  210.    THEN DO
  211.    newmins = newmins + 60        /* positive value for minutes */
  212.    tzohour = tzohour + 1         /* borrow an hour for substraction */
  213.    END
  214.  
  215.  
  216. offset = tzohour  /* set for Your Time Zone */
  217. localhour = hour - offset
  218.  
  219. minusday = 0
  220. IF localhour <0
  221.  THEN  DO
  222.      localhour = localhour + 24
  223.      minusday = 1
  224.  END
  225.  
  226. newhour = localhour
  227.  
  228.  
  229. /* correct for hours and minutes of one character '<10'  */
  230.  
  231. IF localhour <10 THEN newhour = "0" || localhour
  232. IF newmins < 10 THEN newmins = "0" || newmins
  233.  
  234.  
  235. /* create the new time string */
  236.  
  237. newtime = newhour || ":" || newmins ||SUBSTR(timestring,6,3)
  238.  
  239.  
  240. /* Correct for the date  */
  241. IF (minusday = 0) & (gmtflag = 1) THEN 'DATE TOMORROW'
  242. IF (minusday = 1) & (gmtflag = 0) THEN 'DATE YESTERDAY'
  243. /* ELSE leave the data as TODAY */
  244.  
  245.  
  246. /*  AmigaDOS DATE commands  */
  247.  
  248. 'DATE TIME ' || newtime
  249.  
  250. 'DATE'
  251.  
  252. 'SetClock SAVE'
  253. SAY ''
  254.  
  255.  
  256. /* changed in 1.5 from DATE('B')  */
  257. newdate = DATE('I')
  258.  
  259.  
  260.  
  261. /* Compensation for initial clock read                 */
  262. /* Adjust by adding 1 second due to program exec time  */
  263. /* Average exec time was .56 seconds on A3000 25MHz    */
  264.  
  265. adjustment:
  266.  
  267. adjsec = RIGHT(oldtime,2)
  268. adjmin = SUBSTR(oldtime,4,2)
  269. adjhour = LEFT(oldtime,2)
  270.  
  271. adjsec = adjsec + 1
  272.  
  273.    IF adjsec > 59 THEN
  274.       DO
  275.           adjsec = 0
  276.           adjmin = adjmin + 1
  277.             IF adjmin > 60 THEN
  278.                DO
  279.                   adjmin = 0
  280.                   adjhour = adjhour + 1
  281.                      IF adjhour > 23 THEN
  282.                         DO
  283.                            adjhour = 0
  284.                            olddate = olddate + 1
  285.                          END
  286.                END
  287.       END
  288.  
  289. /* ensures values are converted to integer types */
  290.  
  291. adjsec = adjsec + 0
  292. adjmin = adjmin + 0
  293. adjhour = adjhour + 0
  294.  
  295.  
  296.  
  297. adjsecstr$  = adjsec
  298. IF adjsecstr$ = '0' THEN adjsecstr$ = '00'
  299. IF (adjsec > 0 ) & (adjsec < 10) THEN adjsecstr$ = '0' || adjsecstr$
  300.  
  301. adjminstr$ = adjmin
  302. IF adjminstr$ = '0' THEN adjminstr$ = '00'
  303. IF (adjmin > 0 ) & (adjmin < 10) THEN adjminstr$ = '0' || adjminstr$
  304.  
  305. adjhourstr$ = adjhour
  306. IF adjhourstr$ = '0' THEN adjhourstr$ = '00'
  307. IF (adjhour > 0 ) & (adjhour < 10) THEN adjhourstr$ = '0' || adjhourstr$
  308.  
  309. oldtime = adjhourstr$||':'||adjminstr$||':'||adjsecstr$
  310.  
  311. adjustmentend:
  312.  
  313.  
  314.  
  315. oldtimesecs = timeseccalc(oldtime)
  316. newtimesecs = timeseccalc(newtime)
  317.  
  318.  
  319. SAY ' Previous System time was: ' oldtime
  320. SAY ' Current System time is  : ' newtime
  321.  
  322. oldsecs =  (olddate *  86400) + oldtimesecs
  323. newsecs =  (newdate * 86400)  + newtimesecs
  324.  
  325.  
  326. secdiff = newsecs - oldsecs
  327.  
  328. secdiff  =  ABS(secdiff)
  329.  
  330. datediff = TRUNC(secdiff / 86400)
  331.  
  332. compsecdiff = secdiff - (datediff * 86400)
  333.  
  334. diffstr$ = timediff(compsecdiff)
  335.  
  336. /* datediff = newdate - olddate */
  337. /* datediff = ABS(datediff)     */
  338.  
  339. SAY ''
  340. SAY ' Time difference was ' datediff ' days and 'diffstr$
  341. SAY ''
  342.  
  343. EXIT
  344.  
  345. /* ******************  TIME CALC  MODDULE   **************   */
  346.  
  347. /* Function to calc timesecs */
  348. timeseccalc: procedure
  349.  ARG timestring
  350.  
  351.  SECS = RIGHT(timestring,2)
  352.  MINS = SUBSTR(timestring,4,2)
  353.  HOURS = LEFT(timestring,2)
  354.  
  355. timesecs = (HOURS * 3600) + (MINS * 60) + (SECS)
  356.  
  357. RETURN timesecs
  358.  
  359.  
  360. /* Function to converts seconds into HH:MM:SS format */
  361. timediff: procedure
  362.  ARG timenumsecs
  363.  
  364.    hours = timenumsecs % 3600
  365.    hoursecs = hours * 3600
  366.    mins = (timenumsecs - (hoursecs)) % 60
  367.    minsecs = mins * 60
  368.    secs = timenumsecs - hoursecs - minsecs
  369.  
  370.    hourstr$ = hours
  371.    IF (hours < 10) THEN hourstr$ = '0' || hours
  372.    minstr$ = mins
  373.    IF (mins < 10)  THEN minstr$ = '0' || mins
  374.    secstr$ = secs
  375.    IF (secs < 10)  THEN secstr$ = '0' || secs
  376.  
  377.    timeconvstr$ = hourstr$||':'||minstr$||':'||secstr$
  378.  
  379.  
  380.  RETURN timeconvstr$
  381.  
  382.  
  383.  
  384. /* ****************   E N D   O F   P R O G R A M   ****************** */
  385.